//
//  Forgot_password.m
//  K-Music
//
//  Created by CHUN on 16/1/10.
//  Copyright © 2016年 CHUN. All rights reserved.
//

#import "Forgot_password.h"
#import "Forgot_password_T.h"

@interface Forgot_password ()<UITextFieldDelegate,UIAlertViewDelegate>
{
    UITextField * _texfiled1;
    UITextField * _texfiled2;
    
    UIButton * _getBtn;
    int _k;
    NSTimer *_timer;
    NSString *_codestr;
    
}

@end

@implementation Forgot_password


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"忘记密码";
    
    self.view.backgroundColor = RGB(241, 242, 244);
    
    [self tapView];
    
    [self createUI];
    
}

-(void)createUI{
    
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 10
                                                             , WIDTH, 100)];
    
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    
    UILabel *lable = [UILabel lableWithText:CGRectMake(WIDTH/16, 5, 80, 40) Color:RGB(4, 4, 4) Text:@"手机号" Font:nil];
    [view addSubview:lable];
    
    _texfiled1 = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(lable.frame), 5, WIDTH*0.75, 40) ];
    _texfiled1.borderStyle = UITextBorderStyleNone;
    _texfiled1.placeholder = @"手机号";
    _texfiled1.delegate = self;
    [view addSubview:_texfiled1];
    
    UIView * bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 50, WIDTH, 1)];
    bgview.backgroundColor = RGB(241, 242, 244);
    [view addSubview:bgview];
    
    UILabel *lable_code = [UILabel lableWithText:CGRectMake(WIDTH/16, 55, 80, 40) Color:RGB(4, 4, 4) Text:@"验证码" Font:nil];
    [view addSubview:lable_code];
    
    _texfiled2 = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(lable_code.frame), 55, WIDTH*0.75, 40) ];
    _texfiled2.borderStyle = UITextBorderStyleNone;
    _texfiled2.placeholder = @"输入验证码";
    _texfiled2.delegate = self;
    [view addSubview:_texfiled2];
    
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor colorWithRed:240/255.0 green:176/255.0 blue:39/255.0 alpha:1];
    button.tag=101;
    button.layer.cornerRadius=2;
    button.clipsToBounds=YES;
    [button setTitle:@"获取验证码" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(getCode:) forControlEvents:UIControlEventTouchUpInside];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    button.frame = CGRectMake(WIDTH-80-25, 60, 80, 30);
    [view addSubview:button];
    
    
    UIButton * registbutton = [[UIButton alloc] initWithFrame:CGRectMake(WIDTH/32, CGRectGetMaxY(view.frame)+10 ,WIDTH-WIDTH/16, WIDTH/10)];
    
    [registbutton addTarget:self action:@selector(Forgot) forControlEvents:UIControlEventTouchUpInside];
    [registbutton setTitle:@"下一步" forState:UIControlStateNormal];
    registbutton.layer.cornerRadius = 5;
    registbutton.clipsToBounds = YES;
    registbutton.backgroundColor = RGB(240, 176, 39);
    
    [self.view addSubview:registbutton];
    
    //    返回按钮
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"back" highIcon:@"back" target:self action:@selector(back_back)];
    
}

//收键盘手势
- (void)tapView
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(CancelkeyBoard)];
    [self.view addGestureRecognizer:tap];
}

- (void)CancelkeyBoard
{
    [ _texfiled1 resignFirstResponder];
    
    [ _texfiled2 resignFirstResponder];
}


-(void)letmeshow
{
    NSLog(@"我已同意《K歌王APP用户条例》,匹配跳转页面");
}

-(void)agree:(UIButton *)btn
{
    btn.tag++;
    if (btn.tag%2==0) {
        btn.selected=YES;
    }
    else
        btn.selected=NO;
    
}

-(void)getCode:(UIButton *)btn
{
    
    if ([RegularTools validateMobile:_texfiled1.text]==0) {
        
        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"温馨提示"message:@"手机号码有误，请重新输入" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",nil];
        [myAlertView show];
        
    }
    else
    {
        //注册接口
        
        NSString * url = [NSString stringWithFormat:@"http://112.74.72.48:8080/api/messages/send_verify_code?phone=%@",_texfiled1.text];
        AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
        [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            NSLog(@"成功GET --> %@", responseObject);
            
            NSDictionary * dic = [responseObject objectForKey:@"body"] ;
            
            NSLog(@"%@",dic);
            
            [User_information conversion].code = [dic objectForKey:@"verifyCode"];
            
            [User_information conversion].user_phone = [dic objectForKey:@"phone"];
            
            NSString * code = [NSString stringWithFormat:@"手机号 -->%@/n验证码 --> %@",_texfiled1.text,[dic objectForKey:@"verifyCode"]];
            
            [MBProgressHUD showErrorWithStatus:code toView:self.view];
            
            _k=60;
            UIButton *button=(UIButton *)[self.view viewWithTag:101];
            
            [_texfiled1 resignFirstResponder];
            
            _timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(change) userInfo:nil repeats:YES];
            [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
            button.backgroundColor=[UIColor lightGrayColor];
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
            [MBProgressHUD showErrorWithStatus:@"获取失败" toView:self.view];
            
        }];
        
    }
}

-(void)change
{
    _k--;
    UIButton *button=(UIButton *)[self.view viewWithTag:101];
    
    if (_k==0) {
        [_timer invalidate];
        button.backgroundColor=[UIColor yellowColor];
        [button setTitle:@"获取验证码" forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        button.userInteractionEnabled=YES;
        button.backgroundColor = [UIColor colorWithRed:240/255.0 green:176/255.0 blue:39/255.0 alpha:1];
        
    }
    else
    {
        NSString *str=[NSString stringWithFormat:@"重新获取（%zds)",_k];
        button.titleLabel.text=str;
        button.userInteractionEnabled=NO;
        [button setTitle:str forState:UIControlStateNormal];
        button.titleLabel.font=[UIFont systemFontOfSize:10];}
    // NSLog(@"%@",button.titleLabel.text);
}

- (void)Forgot
{
    
    if ([[User_information conversion].code isEqualToString: _texfiled2.text]) {
        //匹配验证码跳转
        [self.navigationController pushViewController:[Forgot_password_T new] animated:YES];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"验证失败" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
    }
}

- (void)back_back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

